from CvPythonExtensions import *

import GerikSpells
from GerikSpells import GerikSpellBase
from GSGroupPolicies import GSAll as GSGroup
from GSHandlerPolicies import GSTargetting as GSHandler
from GSResultsPolicies import GSMission as GSResults

from GSTargetInfoPolicies import GSSquarePlotTarget
from GSTargetInfoPolicies import GSSpecificUnitTarget

from GSGraphicsPolicies import GSPlot

import GerikSpellsUtils

from CivcraftSCUtils import checkAndRemoveEnergy
from CivcraftSCUtils import showNotEnoughEnergyMsg

gc = CyGlobalContext()
localtext = CyTranslator()

class DefilerBase(GerikSpellBase, GSGroup, GSHandler):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)


        self.m_iDefilerType = gc.getInfoTypeForString("UNIT_DEFILER")
            
    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not pCastingUnit.getUnitType() == self.m_iDefilerType:
            return False

        # Show this for all defilers.
        if bVisible:
            return True

        return True

class DarkSwarm(DefilerBase, GSResults):
    def __init__(self):
        DefilerBase.__init__(self)

        # Constants
        self.m_iSwarmEnergyCost = 100
        self.m_iSwarmSize = 1
        self.m_iSwarmRange = 5
        self.m_iSwarmPlotAttachment = gc.getInfoTypeForString("PLOT_ATTACHMENT_DARK_SWARM")

        # Policies
        pSquarePlotTargetPolicy = GSSquarePlotTarget(self.canTargetPlot, self.m_iSwarmSize)
        GSResults.__init__(self, pSquarePlotTargetPolicy)

    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not DefilerBase.canUnitCast(self, pCastingUnit, pPlot, bVisible):
            return False

        return True

    def canTargetPlot(self, pCastingGroup, pCastingPlot, pTargetPlot, bVisible):
        return True

    def getRangePlotList(self, pCastingPlot):
        return GerikSpellsUtils.getRangePlotDistanceList(pCastingPlot, self.m_iSwarmRange)

    def createTargettingStageList(self, bAlt, bShift, bCtrl):
        pPlotTargetPolicy = GSSquarePlotTarget(self.canTargetPlot, self.m_iSwarmSize)
        pPlotTargetPolicy.setRangeMakerCallback(self.getRangePlotList)
        pPlotTargetPolicy.setCheckRange(True)

        pPlotGraphicsPolicy = GSPlot()
        pPlotGraphicsPolicy.setShowRangeGraphics(True)
        pPlotGraphicsPolicy.setCursorType("CURSOR_SINGLE_ATTACK")
        return [(pPlotTargetPolicy, pPlotGraphicsPolicy)]

    def startSpellResults(self, pCastingGroup, pTargetInfo):
        bShowNotEnoughEnergy = False
        
        iPlayer = pCastingGroup.getOwner()
        for pUnit in pCastingGroup.getUnits():
            if checkAndRemoveEnergy(pUnit, self.m_iSwarmEnergyCost):
                for pPlot in pTargetInfo.getAffectedTargetList(pCastingGroup, pCastingGroup.plot()):
                    pPlot.attachPlotAttachment(iPlayer, self.m_iSwarmPlotAttachment)
            else:
                bShowNotEnoughEnergy = True

        if bShowNotEnoughEnergy:
            showNotEnoughEnergyMsg(iPlayer)
            
class Plague(DefilerBase, GSResults):
    def __init__(self):
        DefilerBase.__init__(self)

        # Constants
        self.m_iPlagueEnergyCost = 150
        self.m_iPlagueSize = 1
        self.m_iPlagueRange = 5
        self.m_iPlagueUnitAttachment = gc.getInfoTypeForString("UNIT_ATTACHMENT_PLAGUE")

        self.m_iResearchNeeded = gc.getInfoTypeForString("PROJECT_ZERG_DEFILER_PLAGUE")
        self.m_iResearchBuildingToBuildAt = gc.getProjectInfo(self.m_iResearchNeeded).getBuildingToBuildAt()
        self.m_szNotValidText = localtext.getText( "TXT_KEY_CIVCRAFT_REQUIRES_RESEARCH_AT", (gc.getProjectInfo(self.m_iResearchNeeded).getDescription(),
                                                                                             gc.getBuildingInfo(self.m_iResearchBuildingToBuildAt).getDescription()) )

        # Policies
        pSquarePlotTargetPolicy = GSSquarePlotTarget(self.canTargetPlot, self.m_iPlagueSize)
        GSResults.__init__(self, pSquarePlotTargetPolicy)

    def getHelpText(self, pGroup, pPlot):
        bValid = self.canGroupCast(pGroup, pPlot, False)
        if not bValid:
            return self.m_szNotValidText

    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not DefilerBase.canUnitCast(self, pCastingUnit, pPlot, bVisible):
            return False

        if bVisible:
            return True

        # Check if they have the research
        if not gc.getPlayer( pCastingUnit.getOwner() ).hasBuiltProject( self.m_iResearchNeeded ):
            return False

        return True

    def canTargetPlot(self, pCastingGroup, pCastingPlot, pTargetPlot, bVisible):
        return True

    def getRangePlotList(self, pCastingPlot):
        return GerikSpellsUtils.getRangePlotDistanceList(pCastingPlot, self.m_iPlagueRange)

    def createTargettingStageList(self, bAlt, bShift, bCtrl):
        pPlotTargetPolicy = GSSquarePlotTarget(self.canTargetPlot, self.m_iPlagueSize)
        pPlotTargetPolicy.setRangeMakerCallback(self.getRangePlotList)
        pPlotTargetPolicy.setCheckRange(True)

        pPlotGraphicsPolicy = GSPlot()
        pPlotGraphicsPolicy.setShowRangeGraphics(True)
        pPlotGraphicsPolicy.setCursorType("CURSOR_SINGLE_ATTACK")
        return [(pPlotTargetPolicy, pPlotGraphicsPolicy)]

    def startSpellResults(self, pCastingGroup, pTargetInfo):
        bShowNotEnoughEnergy = False
        
        iPlayer = pCastingGroup.getOwner()
        for pUnit in pCastingGroup.getUnits():
            if checkAndRemoveEnergy(pUnit, self.m_iPlagueEnergyCost):
                for pPlot in pTargetInfo.getAffectedTargetList(pCastingGroup, pCastingGroup.plot()):
                    for iUnit in range(pPlot.getNumUnits()):
                        pLoopUnit = pPlot.getUnit(iUnit)
                        pLoopUnit.attachUnitAttachment(iPlayer, self.m_iPlagueUnitAttachment)
            else:
                bShowNotEnoughEnergy = True

        if bShowNotEnoughEnergy:
            showNotEnoughEnergyMsg(iPlayer)
